package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SystemNotifyMessage;
import com.ruoyi.system.service.ISystemNotifyMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 站内信消息Controller
 *
 * @author ruoyi
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/system/notify-message")
public class SystemNotifyMessageController extends BaseController
{
    @Autowired
    private ISystemNotifyMessageService systemNotifyMessageService;

    /**
     * 查询站内信消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:notify-message:list')")
    @GetMapping("/list")
    public TableDataInfo list(SystemNotifyMessage systemNotifyMessage)
    {
        startPage();
        List<SystemNotifyMessage> list = systemNotifyMessageService.selectSystemNotifyMessageList(systemNotifyMessage);
        return getDataTable(list);
    }

    /**
     * 导出站内信消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:notify-message:export')")
    @Log(title = "站内信消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemNotifyMessage systemNotifyMessage)
    {
        List<SystemNotifyMessage> list = systemNotifyMessageService.selectSystemNotifyMessageList(systemNotifyMessage);
        ExcelUtil<SystemNotifyMessage> util = new ExcelUtil<SystemNotifyMessage>(SystemNotifyMessage.class);
        util.exportExcel(response, list, "站内信消息数据");
    }

    /**
     * 获取站内信消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notify-message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(systemNotifyMessageService.selectSystemNotifyMessageById(id));
    }

    /**
     * 新增站内信消息
     */
    @PreAuthorize("@ss.hasPermi('system:notify-message:add')")
    @Log(title = "站内信消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SystemNotifyMessage systemNotifyMessage)
    {
        return toAjax(systemNotifyMessageService.insertSystemNotifyMessage(systemNotifyMessage));
    }

    /**
     * 修改站内信消息
     */
    @PreAuthorize("@ss.hasPermi('system:notify-message:edit')")
    @Log(title = "站内信消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SystemNotifyMessage systemNotifyMessage)
    {
        return toAjax(systemNotifyMessageService.updateSystemNotifyMessage(systemNotifyMessage));
    }

    /**
     * 删除站内信消息
     */
    @PreAuthorize("@ss.hasPermi('system:notify-message:remove')")
    @Log(title = "站内信消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(systemNotifyMessageService.deleteSystemNotifyMessageByIds(ids));
    }


    // ========== 查看自己的站内信 ==========
    @GetMapping("/my-page")
    @Operation(summary = "获得我的站内信分页")
    public TableDataInfo getMyMyNotifyMessagePage(SystemNotifyMessage systemNotifyMessage)
    {
        startPage();
        systemNotifyMessage.setUserId(getUserId());
        List<SystemNotifyMessage> list = systemNotifyMessageService.selectSystemNotifyMessageList(systemNotifyMessage);
        return getDataTable(list);
    }

    @PutMapping("/update-read")
    @Operation(summary = "标记站内信为已读")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public AjaxResult updateNotifyMessageRead(@RequestParam("ids") List<Long> ids) {
        systemNotifyMessageService.updateNotifyMessageRead(ids, getUserId());
        return success(Boolean.TRUE);
    }

    @PutMapping("/update-all-read")
    @Operation(summary = "标记所有站内信为已读")
    public AjaxResult updateAllNotifyMessageRead() {
        systemNotifyMessageService.updateAllNotifyMessageRead(getUserId());
        return success(Boolean.TRUE);
    }

    @GetMapping("/get-unread-list")
    @Operation(summary = "获取当前用户的最新站内信列表，默认 10 条")
    @Parameter(name = "size", description = "5")
    public AjaxResult getUnreadNotifyMessageList(
            @RequestParam(name = "size", defaultValue = "5") Integer size) {
        List<SystemNotifyMessage> list = systemNotifyMessageService.getUnreadNotifyMessageList(
                getUserId(), size);
        return success(list);
    }

    @GetMapping("/get-nopush-list")
    @Operation(summary = "获取当前用户的待推送列表,当前时间后")
    @Parameter(name = "createTime", description = "当前时间")
    public AjaxResult getNoPushNotifyMessageByTime(
            @RequestParam(name = "createTime") String createTime) {
        List<SystemNotifyMessage> list = systemNotifyMessageService.getNoPushNotifyMessageByTime(
                getUserId(), createTime);
        return success(list);
    }

    @GetMapping("/get-unread-count")
    @Operation(summary = "获得当前用户的未读站内信数量")
    public AjaxResult getUnreadNotifyMessageCount() {
        int count = systemNotifyMessageService.getUnreadNotifyMessageCount(getUserId());
        return success(count);
    }

    @PutMapping("/update-push")
    @Operation(summary = "标记推送标记为已推")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public AjaxResult updateNotifyMessagePush(@RequestParam("ids") List<Long> ids) {
        systemNotifyMessageService.updateNotifyMessagePush(ids, getUserId());
        return success(Boolean.TRUE);
    }
}
